//
//  ViewController.m
//  format
//
//  Created by longlong on 2022/11/9.
//

#import "ViewController.h"
@interface ViewController()
@property (weak) IBOutlet NSTextField *inputTextField;
@property (unsafe_unretained) IBOutlet NSTextView *outputTextView;
@property (unsafe_unretained) IBOutlet NSTextView *supergroupTextView;
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}
- (IBAction)touchIn:(NSButton *)sender {
    NSString *inputString =  _inputTextField.stringValue;
    // 1、格式化
    for (int i = 1; i < 100; i++) {
        // （1）、清除干扰
        NSString *oldString = [NSString stringWithFormat:@"+ %d ",i];
        NSString *newString = [NSString stringWithFormat:@"+.%d ",i];
        inputString = [inputString stringByReplacingOccurrencesOfString:oldString withString:newString];
        // （2）、插入换行
        NSString *fromString = [NSString stringWithFormat:@" %d ",i];
        NSString *toString = [NSString stringWithFormat:@"\n %d ",i];
        inputString = [inputString stringByReplacingOccurrencesOfString:fromString withString:toString];
    }
    _outputTextView.string = inputString;

    // 2、找出堆栈中超话 问题类
    NSMutableArray *result = [[NSMutableArray alloc]init];
    NSArray *iosClassList = [self getClassList];
    NSArray *inputList = [inputString componentsSeparatedByString:@"\n"];
    for (__strong NSString *className in iosClassList) {
        // (1)分类 只取后半部分
        if([className containsString:@"+"]){
            NSArray *addNameList = [className componentsSeparatedByString:@"+"];
            if(addNameList.count > 1){
                className = addNameList[1];
            }
        }
        // (2)判断是否包含此类
        if([inputString containsString:className]){
            for (NSString *input in inputList) {
                if([input containsString:className]){
                    [result addObject:input];
                }
            }
        }
    }
    NSArray *newResult = [result valueForKeyPath:@"@distinctUnionOfObjects.self"];
    _supergroupTextView.string = [newResult componentsJoinedByString:@"\n"];
    
    // 3、暗黑模式
    _supergroupTextView.textColor = [NSColor colorNamed:@"titleColor"];
    _outputTextView.textColor = [NSColor colorNamed:@"titleColor"];
    
//    @"find ~/Library/ -iname \"**\""
//    @"git blame -L n,m filename";
}

-(NSArray *)getClassList{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ios_class_info" ofType:nil];
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]];
    NSString *stringData = [[ NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return [stringData componentsSeparatedByString:@"\n"];
}
@end
